﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using VirtualPress.Tool.Framework;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    [Table("T_PublishApply")]
    public class T_PublishApply : IEntity<T_PublishApply>
    {
        #region Property
        [Column(TypeName = "int")]
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        /// <summary>
        /// 书名
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Name { get; set; }

        /// <summary>
        /// 作者
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string Authors { get; set; }

        /// <summary>
        /// 领域
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string Field { get; set; }

        /// <summary>
        /// 简介
        /// </summary>
        [Column(TypeName = "text")]
        public string Introduction { get; set; }

        /// <summary>
        /// 出书方式
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(32)]
        public string Way { get; set; }

        /// <summary>
        /// 联系电话
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(32)]
        public string Phone { get; set; }

        /// <summary>
        /// 联系人
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(16)]
        public string Contact { get; set; }

        /// <summary>
        /// 联系邮箱
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string Mail { get; set; }

        /// <summary>
        /// 申请时间
        /// </summary>
        [Column(TypeName = "datetime")]
        public DateTime CreateDate { get; set; }
        #endregion

        #region Methods
        public override T_PublishApply Find(System.Linq.Expressions.Expression<Func<T_PublishApply, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.PublishApply.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.Authors = item.Authors;
                    this.Contact = item.Contact;
                    this.CreateDate = item.CreateDate;
                    this.Field = item.Field;
                    this.Introduction = item.Introduction;
                    this.Mail = item.Mail;
                    this.Name = item.Name;
                    this.Phone = item.Phone;
                    this.Way = item.Way;
                    return this;
                }
                return null;
            }
        }
        public override T_PublishApply Find(string key)
        {
            int id = key.ToInt32();
            return Find(c => c.Id == id);
        }
        public override bool Any(System.Linq.Expressions.Expression<Func<T_PublishApply, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.PublishApply.Any(where);
            }
        }
        #endregion
    }
}